#!/bin/bash
# Author: Redbrother
# Email: 728083668@qq.com
# WebSite: https://help.ubuntu.com/community/LiveCDCustomizationFromScratch
# license: None
# data :2019 10
# scripts in /usr/share/debootstrap/scripts

############################################

export ARCH=amd64
export RELEASE=jammy
export installer_url=https://mirrors.ustc.edu.cn/ubuntu/
export IMAGE=ubuntu-$RELEASE
export CURDIR=$(cd `dirname $0`;pwd)
export CHROOT=$CURDIR/squashfs-root
export LC_ALL=C
export DEBIAN_FRONTEND=noninteractive
export APT_LISTCHANGES_FRONTEND=none

set -e
#自定义版本项

############################################


############################################
function do_prep()
{
if [ "`whoami`" != "root" ]; then
	echo "User must be root 需要root权限."
	exit 0
fi

apt install debootstrap xorriso genisoimage squashfs-tools isolinux syslinux-efi grub-pc-bin grub-efi-amd64-bin grub-efi-ia32-bin mtools dosfstools -y | tee -a /var/log/ubuntulive.log
if [ "$?" != "0" ]; then
	echo "Commands required: mkisofs, squashfs-tools ,wget."
	exit 0
fi
if [ "`which mkisofs`" == "" ]; then
	ln -s /usr/bin/genisoimage /usr/bin/mkisofs
fi

}

############################################
function do_base()
{
### debootstrap a base live
umount "$CURDIR"/squashfs-root/* 2>/dev/null || true
rm -rf $CURDIR/squashfs-root
mkdir -p $CURDIR/squashfs-root
debootstrap --arch=$ARCH $RELEASE $CURDIR/squashfs-root $installer_url

### config apt and hosts base
cd $CURDIR/squashfs-root
echo "deb $installer_url $RELEASE main restricted universe multiverse"          >  etc/apt/sources.list
echo "deb $installer_url $RELEASE-updates main restricted universe multiverse"  >> etc/apt/sources.list
echo "deb $installer_url $RELEASE-security main restricted universe multiverse" >> etc/apt/sources.list
echo 'APT::Install-Recommends "false";' > etc/apt/apt.conf
chroot . chmod a+s /bin/ping
cp -f /etc/hosts etc/hosts
mkdir -p run/NetworkManager/
cp -f /etc/resolv.conf etc/
chroot . echo "root:password" | chpasswd
echo "livecd" | tee "$CURDIR/squashfs-root/etc/hostname"

}

############################################
function do_mountx()
{
### mounts
cd $CURDIR/squashfs-root
mount --bind /dev/   $CHROOT/dev
mount -t proc proc   $CHROOT/proc
mount -t sysfs sysfs $CHROOT/sys
mkdir -p $CHROOT/dev/pts
mount -o bind /dev/pts $CHROOT/dev/pts/
chroot . rm -f /sbin/initctl
chroot . dbus-uuidgen > /var/lib/dbus/machine-id
chroot . dpkg-divert --local --rename --add /sbin/initctl
chroot . ln -s /bin/true /sbin/initctl

}

############################################
function do_upgrade()
{
### upgrade and kernels and liveCD necessary
cd $CURDIR/squashfs-root
chroot . apt-get update
chroot . apt-get upgrade --yes
chroot . locale-gen zh_CN.UTF-8
chroot . locale-gen en_US.UTF-8
### kernels and network
# kernels and network
echo -e "install kernels and network …… "
export DEBIAN_FRONTEND=noninteractive
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends linux-image-6.8.0-40-generic linux-modules-6.8.0-40-generic linux-modules-extra-6.8.0-40-generic locales dbus initramfs-tools xserver-xorg xserver-xorg-core xinit sudo bash-completion console-setup isc-dhcp-client net-tools network-manager --yes
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends grub-efi grub-pc-bin grub-efi-amd64-bin grub-efi-ia32-bin --yes
# Livecd necessary
echo -e "install Livecd necessary  "
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends ubuntu-standard casper discover laptop-detect os-prober --yes

}

############################################
function do_usestools()
{
cd $CURDIR/squashfs-root
### useful tools
# useful tools
echo -e "install useful tools 1 "
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends tar wget curl vim nano git gvfs lzma unzip zip p7zip --yes
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends rsync debootstrap squashfs-tools xorriso genisoimage mtools dosfstools --yes
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends parted gparted partclone mdadm lvm2 expect zssh openssh-server openssh-sftp-server openssh-client --yes
DEBIAN_FRONTEND=noninteractive chroot . apt purge ubuntu-advantage-tools -y
echo "Port 22"  >> etc/ssh/sshd_config
echo "PermitRootLogin yes"  >> etc/ssh/sshd_config
echo "PasswordAuthentication yes"  >> etc/ssh/sshd_config
chroot . update-initramfs -u
cp -f "$CURDIR"/tools/bin/*  $CURDIR/squashfs-root/usr/local/bin
chroot . chmod +755 -R /usr/local/bin
chroot . chmod +644 -R /boot
chroot . chmod +755 -R /root
chroot . rm -f etc/localtime
chroot . ln -sf /usr/share/zoneinfo/Asia/Shanghai etc/localtime
echo '0.0 0 0' >  etc/adjtime
echo '0'       >> etc/adjtime
echo 'LOCAL'   >> etc/adjtime
echo 'Asia/Shanghai'             >  etc/timezone
echo 'export TZ=Asia/Shanghai'   >> etc/profile
echo 'export LANG=zh_CN.UTF-8'   >> etc/profile
echo 'export LC_ALL=zh_CN.UTF-8' >> etc/profile
echo 'export LANGUAGE=zh_CN.UTF-8'   >> $CHROOT/etc/profile
echo '. /etc/bash_completion'   >> etc/bash.bashrc
touch root/.Xauthority
}

############################################
function do_timezone()
{
cd $CURDIR/squashfs-root
### zh_CN language packs and timezone
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends language-pack-zh-hans language-pack-zh-hans-base --yes
chroot . cp -rf usr/share/locale-langpack/zh_CN/LC_MESSAGES/* usr/share/locale/zh_CN/LC_MESSAGES/
chroot . rm -rf usr/share/locale-langpack/

### user settings
mkdir -p home/ubuntu/Desktop
mkdir -p home/ubuntu/.config
cp -rf "$CURDIR"/tools/ubuntu/config/* home/ubuntu/.config/
chown -R 999:999 home/ubuntu/Desktop/
chown -R 999:999 home/ubuntu/.config/
chown -R 999:999 home/ubuntu/
chmod  +766 -R home/ubuntu/
chroot . rm -f home/ubuntu/.config/user-dirs.*

### auto login
mkdir -p etc/lightdm/lightdm.conf.d
mkdir -p usr/share/lightdm/lightdm.conf.d
echo '[SeatDefaults]'           >  etc/lightdm/lightdm.conf.d/90-default.conf
echo 'autologin-user=ubuntu'    >> etc/lightdm/lightdm.conf.d/90-default.conf
echo 'autologin-user-timeout=0' >> etc/lightdm/lightdm.conf.d/90-default.conf
echo 'autologin-guest=false'    >> etc/lightdm/lightdm.conf.d/90-default.conf
echo 'allow-guest=false'        >> usr/share/lightdm/lightdm.conf.d/60-lightdm-gtk-greeter.conf
chroot . xsession-config

}

############################################
function do_fctix()
{
cd $CURDIR/squashfs-root
# fcitx-googlepinyin
echo -e "install fcitx-googlepinyin and fonts  "
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends fonts-ubuntu fonts-wqy-microhei fcitx fcitx-ui-classic fcitx-frontend-gtk2 fcitx-config-gtk fcitx-googlepinyin --yes
# fcitx config
mkdir -p etc/X11/Xsession.d/
mkdir -p etc/xdg/autostart/
echo 'export GTK_IM_MODULE="xim"'    >  etc/X11/Xsession.d/96fcitx_start
echo 'export XMODIFIERS="@im=fcitx"' >> etc/X11/Xsession.d/96fcitx_start
cp -f usr/local/bin/97pinyin*      etc/X11/Xsession.d/
cp -f usr/local/bin/fcitx-pinyin*  usr/bin
cp "$CURDIR"/tools/settings/*.desktop               usr/share/applications
cp "$CURDIR"/tools/settings/fcitx-config.desktop    etc/xdg/autostart/

}

############################################
function do_desktop()
{
### mate desktop environment
cd $CURDIR/squashfs-root
echo -e " mate desktop environment "
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends lightdm mate-desktop-environment-core epiphany-browser pluma engrampa eom mate-terminal --yes
DEBIAN_FRONTEND=noninteractive chroot . apt purge ubuntu-advantage-tools -y
# cd usr/share/themes/ && rm -rf `ls | grep -v Default Adwaita Black* Blue* Green* Menta* ` && cd ../../..
cd usr/share/themes/ && rm -rf ContrastHigh* Clearlook* Crux* eOS* HighContrast* Industria* Mist* Raleigh* Redmond* Traditional*  &&  cd ../../..

### add gonme applications Links
chroot . rm -f /usr/bin/gedit
chroot . rm -f /usr/bin/nautilus
chroot . rm -f /usr/bin/gnome-terminal
chroot . rm -f /usr/bin/file-roller
chroot . rm -f /usr/bin/eog
ln -s pluma usr/bin/gedit
ln -s caja  usr/bin/nautilus
ln -s eom   usr/bin/eog
ln -s engrampa      usr/bin/file-roller
ln -s mate-terminal usr/bin/gnome-terminal
###  modify some desktop
cd usr/share/applications
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' engrampa.desktop
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' mate-terminal.desktop
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' caja-browser.desktop
sed -i 's/Exec=fcitx/Exec=im-config/' fcitx.desktop
sed -i 's/Settings/System/' fcitx-con*.desktop
sed -i 's/NoDisplay\=true//' python*.desktop
sed -i 's/System\;//'  mate-terminal.desktop
sed -i 's/System\;//'  caja-browser.desktop
sed -i 's/MATE\ 之眼//' eom.desktop
cd ../../../

}

############################################
function do_cleanup()
{
### clean up and umountx
cd $CURDIR/squashfs-root
### Remove upgraded, old linux-kernels if more than one
#ls /boot/vmlinuz-*.*.**-**-generic > list.txt
#sum=$(cat list.txt | grep '[^ ]' | wc -l)
#if [ $sum -gt 1 ]; then
#dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs apt -y purge
#fi
#rm list.txt
chroot . apt install --reinstall network-manager -y
chroot . rm -f etc/init/tty3.conf etc/init/tty4.conf etc/init/tty5.conf
chroot . rm -f network-manager*.deb
#chroot . rm -f etc/resolv.conf
chroot . apt-get clean
chroot . apt-clean
### umountx
chroot . apt clean
chroot . rm -rf tmp/*
chroot . rm -f vmlinu*  initrd.im*
chroot . rm -f /var/lib/dbus/machine-id
chroot . rm -f /sbin/initctl
chroot . dpkg-divert --rename --remove /sbin/initctl
umount -lf sys/
umount -lf proc/
umount -lf dev/pts/
umount -lf dev/
cd ..

}
do_prep
do_base
do_mountx
do_upgrade
do_usestools
do_timezone
do_fctix
do_desktop
do_cleanup

############################################

# ubuntu live base system is Success
  echo -e "ubuntu live base system is successful, you can diy it in $CURDIR/squashfs-root contents "
  echo -e ". Do you want to make an ISO image ? [y/n] "
  echo -e "you can run ' sudo bash mkiso.sh ' to build an iso LiveCD;successful "

